package com.javaDemo.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

/**
* 设置样式
*/
public class ExcelStyleHandler implements CellWriteHandler {


  @Override
  public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

  }

  @Override
  public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

  }

    /**
     * 设置头部填充数据的格式
     * @param writeSheetHolder
     * @param writeTableHolder Nullable.It is null without using table writes.
     * @param cellDataList     Nullable.It is null in the case of add header.There may be several when fill the data.
     * @param cell
     * @param head             Nullable.It is null in the case of fill data and without head.
     * @param relativeRowIndex Nullable.It is null in the case of fill data.
     * @param isHead           It will always be false when fill data.
     */
  @Override
  public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    // 如果是填充headerMap的单元格（通常是表头），需要保留原始样式
    if (cell != null && cell.getSheet() != null) {
      Sheet sheet = cell.getSheet();
      Workbook workbook = sheet.getWorkbook();
      
      // 获取模板中的样式
      int rowIndex = cell.getRowIndex();
      int columnIndex = cell.getColumnIndex();
      
      // 创建一个新的单元格样式，复制模板样式
      CellStyle newStyle = workbook.createCellStyle();
      
      // 获取当前单元格样式
      CellStyle currentStyle = cell.getCellStyle();
      if (currentStyle != null) {
        // 复制所有样式属性
        newStyle.cloneStyleFrom(currentStyle);
        
        // 保留原始字体设置
        Font originalFont = workbook.getFontAt(currentStyle.getFontIndex());
        if (originalFont != null) {
          Font newFont = workbook.createFont();
          // 复制字体属性
          newFont.setBold(originalFont.getBold());
          newFont.setItalic(originalFont.getItalic());
          newFont.setFontHeight(originalFont.getFontHeight());
          newFont.setUnderline(originalFont.getUnderline());
          newFont.setStrikeout(originalFont.getStrikeout());
          // 如果原始字体不是Calibri，则保留原始字体名称
          if (originalFont.getFontName() != null && !originalFont.getFontName().equals("Calibri")) {
            newFont.setFontName(originalFont.getFontName());
          }
          newStyle.setFont(newFont);
        }
        
        // 应用样式到单元格
        cell.setCellStyle(newStyle);
      }
    }
  }
}
